package com.atguigu.tingshu.search.api;

import co.elastic.clients.elasticsearch.ml.Category;
import com.atguigu.tingshu.common.login.AuthLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.query.album.AlbumTrackListVo;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.bus.event.PathDestinationFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search")
@SuppressWarnings({"all"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    /**
     * 站内专辑检索
     *
     * @param albumIndexQuery 查询条件
     * @retrun albumSearchResponseVo 返回Vo视图对象
     */
    @Operation(summary = "专辑检索")
    @PostMapping("/albumInfo")
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery albumIndexQuery) {
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }

    /**
     * 查询指定一级分类下的热门专辑排行
     *
     * @param category1Id 一级分类ID
     * @return mapList 三级分类map与热度top6专辑集合
     */
    @Operation(summary = "查询指定一级分类下热门专辑排行(limit 6)")
    @GetMapping("/albumInfo/channel/{category1Id}")
    public Result<List<Map<String, Object>>> channel(@PathVariable Long category1Id) {
        List<Map<String, Object>> mapList = searchService.channel(category1Id);
        return Result.ok(mapList);
    }


    /**
     * 根据用户录入关键字查询索引库自动匹配补全关键字
     *
     * @param keyword 用户输入关键字
     * @return keywordList 返回完整字符串集合
     */
    @Operation(summary = "根据用户录入关键字查询索引库自动匹配补全关键字")
    @GetMapping("/albumInfo/completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable String keyword) {
        List<String> keywordList = searchService.completeSuggest(keyword);
        return Result.ok(keywordList);
    }

    /**
     * 更新所有分类下排行榜
     * @todo: 定时任务
     */
    @Operation(summary = "更新所有分类下排行榜-手动调用")
    @GetMapping("/albumInfo/updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking() {
        searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }

    /**
     * 获取排行榜
     * @param category1Id
     * @param dimension
     * */
    @Operation(summary ="获取排行榜")
    @GetMapping("/albumInfo/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndexVo>> findRankingList(@PathVariable Long category1Id, @PathVariable String dimension){
        List<AlbumInfoIndexVo> rankingList = searchService.findRankingList(category1Id,dimension);
        return Result.ok(rankingList);
    }




    /**
     * 上架专辑信息(用于上架服务调试): 保存至索引库
     * -并不需要做幂等性处理,重复提交会覆盖前值
     *
     * @param albumId 专辑ID
     * @todo: 异步处理
     */
    @Operation(summary = "上架专辑-导入索引库(用于上架测试)")
    @GetMapping("/albumInfo/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable Long albumId) {
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

    /**
     * 下架专辑信息(用于下架服务调试): 删除索引库
     *
     * @param albumId 专辑ID
     */
    @Operation(summary = "下架专辑-删除索引库(用于下架测试)")
    @GetMapping("/albumInfo/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable Long albumId) {
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }
}

